package com.brackets.stringparser;

/**
 * @author Dmitry
 */
public class StringLayout {

    private final int[] indexes;

    public StringLayout(int[] indexes) {
        this.indexes = indexes;
    }

    public int getLineNumber(int position) {
        if (position < 0) {
            throw new IllegalArgumentException("Position cannot be less then 0. Found " + position);
        }
        for (int i = 0; i < indexes.length; i++) {
            if (position < indexes[i]) {
                return i-1;
            }
        }
        return 0;
    }

    public int getOffsetInLine(int position) {
        if (position < 0) {
            throw new IllegalArgumentException("Position cannot be less then 0. Found " + position);
        }
        int lineNumber = getLineNumber(position);
        int lineStart = indexes[lineNumber];

        return position - lineStart;
    }
}
